// 通用事件
var commonEvents = {

    // 设置事件
    setOrdinaryEvents: function () {
        if (document.getElementById("back")) {
            document.getElementById("back").onclick = function () {
                history.go(-1)
            }
        }
    },
    popupView: function(headerStr, bodyStr) {
        var modalObj = document.getElementById("myModal")
        commonEvents.openPopupView(headerStr, bodyStr, modalObj)
        $('#myModal').ready(function () {
            var popup_h = $(this).find(".modal-content").height() + 2
            var enable_win_h = $(window).height()
            if (popup_h < enable_win_h) {
                var margin_top = (enable_win_h - popup_h) * 0.5
                $("#myModal .modal-dialog").css("margin-top",margin_top)
            } else {
                $("#myModal .modal-dialog").css("margin-top",0)
            }
            $(document).on("click","#myModal #modal_okBtn",function () {
                commonEvents.ok()
                commonEvents.closePopupView(modalObj)
                commonEvents.okCallBack()
            })
            $("#myModal #modal_cancelBtn").on("click",function (event) {
                commonEvents.closePopupView(modalObj);
            })
            $("#myModal #modal_closeBtn").on("click",function () {
                commonEvents.closePopupView(modalObj);
            })
        })
    },
    openPopupView: function(headerStr, bodyStr, modalObj) {
        $("#myModal .modal-header #modalLabel").html(headerStr)
        $("#myModal .modal-body").html(bodyStr)
        $("#myModal").after("<div class='modal-backdrop fade in'></div>")
        $("#myModal").addClass("in")
        modalObj.setAttribute("aria-hidden","false")
        $('#myModal').show()
    },
    closePopupView: function(modalObj) {
        $("#myModal").removeClass("in")
        setTimeout(function () {
            $('#myModal').hide()
            $("#myModal .modal-header #modalLabel").html("")
            $("#myModal .modal-body").html("")
            modalObj.setAttribute("aria-hidden","true")
            $(".modal-backdrop").remove()
        },500)

    },
    validateCompleteness: function () {
        var not_null_objs = $(".not-null")
        var n = 0
        not_null_objs.each(function () {
            if (trim($(this).val()) == "") n++
        })
        if (n == 0) {
            return true
        }
        return false
    },
    validateString: function (typeObj,value) {
        var phone_reg = /^1[34578]\d{9}$/
        var psw_reg = /^[^\s]{6,20}$/

        if (typeObj.toLowerCase().indexOf("phone") >= 0 ) {
            if (phone_reg.test(value)) {
                return true
            }
            return false
        } else if (typeObj.toLowerCase().indexOf("password") >= 0 || typeObj.toLowerCase().indexOf("psd") >= 0) {
            if (psw_reg.test(value)) {
                return true
            }
            return false
        }
    },
    ok: function () {
    },
    okCallBack: function () {
        commonEvents.ok = function(){
        }
    }
}
// 获取当前页面的名字标识
function getPageName() {
    if ($("#app-container").length > 0 ) {
        return $("#app-container").attr("myApp")
    }
}
function getPageView() {
    if ($("#app-container").length > 0 ) {
        return $("#app-container").attr("myView")
    }
}


function showErrorView(alertText) {
    var popup_error;
    if (alertText == "" || alertText == null) {
        popup_error = "<div class='POPUP-ERROR'>没有获取到数据</div>"
    } else {
        popup_error = "<div class='POPUP-ERROR'>" + alertText + "</div>";
    }
    $("body").append(popup_error);
    var winHeight = $(window).height();
    var popHeight = $(".POPUP-ERROR").height();
    var heightPosition = (winHeight - popHeight) / 2;
    $(".POPUP-ERROR").css("top",heightPosition);

    $(".POPUP-ERROR").fadeIn(2000,function() {
        $(".POPUP-ERROR").fadeOut(2000);
        setTimeout(function() {
            $(".POPUP-ERROR").remove();
        },2000);
    });
}


function setDateTimePicker() {
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        daysMin:  ["日", "一", "二", "三", "四", "五", "六"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        meridiem: "",
        today: "今天",
        clear: "清除"
        //format: "yyyy年mm月dd日",
        //titleFormat: "yyyy年mm月",
        //weekStart: 1
    };
    $('.dateTimePicker').datetimepicker({
        language: 'zh-CN',          // 语言
        format: 'yyyy-mm-dd',       // 日期格式
        weekStart: 1,               // 周一
        todayBtn:  true,            // 是否显示今日按钮
        autoclose: true,            // 选择一个日期后自动关闭时间选择器
        todayHighlight: true,       // 今天高亮显示
        startView: 2,               // 0 hour, 1 day, 2 month, 3 year, 4 10year
        minView: 2,                 // 所能提供的最精准的时间选择视图 0:hour, 1:day, 2:month, 3:year, 4:10 year
        //maxView: 1,               // 所能提供的最高的选额范围视图 4:10year(默认)
        forceParse: true,           // 是否强制解析输入框中的值
        keyboardNavigation: true,   // 是否允许通过方向键改变日期
        pickerPosition: "bottom-right"
    });
}

function setDateTimePicker_new() {
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        daysMin:  ["日", "一", "二", "三", "四", "五", "六"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        meridiem: "",
        today: "今天",
        clear: "清除"
        //format: "yyyy年mm月dd日",
        //titleFormat: "yyyy年mm月",
        //weekStart: 1
    };
    $('.dateTimePickerAll').datetimepicker({
        language: 'zh-CN',          // 语言
        format: 'yyyy-mm-dd hh:ii:ss',       // 日期格式
        weekStart: 1,               // 周一
        todayBtn:  true,            // 是否显示今日按钮
        autoclose: true,            // 选择一个日期后自动关闭时间选择器
        todayHighlight: true,       // 今天高亮显示
        startView: 2,               // 0 hour, 1 day, 2 month, 3 year, 4 10year
        // minView: 2,                 // 所能提供的最精准的时间选择视图 0:hour, 1:day, 2:month, 3:year, 4:10 year
        //maxView: 1,               // 所能提供的最高的选额范围视图 4:10year(默认)
        forceParse: true,           // 是否强制解析输入框中的值
        keyboardNavigation: true,   // 是否允许通过方向键改变日期
        pickerPosition: "bottom-right"
    });
}

function setTimePicker() {
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        daysMin:  ["日", "一", "二", "三", "四", "五", "六"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        meridiem: "",
        today: "今天",
        clear: "清除"
        //format: "yyyy年mm月dd日",
        //titleFormat: "yyyy年mm月",
        //weekStart: 1
    };
     $('.form_datetime').datetimepicker({
        language: 'zh-CN',          // 语言
        format: 'hh:ii',       // 日期格式
        weekStart: 1,               // 周一
        todayBtn:  true,            // 是否显示今日按钮
        autoclose: true,            // 选择一个日期后自动关闭时间选择器
        todayHighlight: true,       // 今天高亮显示
        startView: 1,               // 0 hour, 1 day, 2 month, 3 year, 4 10year
        minView: 0,                 // 所能提供的最精准的时间选择视图 0:hour, 1:day, 2:month, 3:year, 4:10 year
        //maxView: 1,               // 所能提供的最高的选额范围视图 4:10year(默认)
        forceParse: true,           // 是否强制解析输入框中的值
        keyboardNavigation: true,   // 是否允许通过方向键改变日期
        pickerPosition: "bottom-right"
    });
}

function setDatePicker() {
    $.fn.datetimepicker.dates['zh-CN'] = {
        days: ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
        daysShort: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        daysMin:  ["日", "一", "二", "三", "四", "五", "六"],
        months: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        monthsShort: ["1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月"],
        meridiem: "",
        today: "今天",
        clear: "清除"
        //format: "yyyy年mm月dd日",
        //titleFormat: "yyyy年mm月",
        //weekStart: 1
    };
    $('.datePicker').datetimepicker({
        language: 'zh-CN',          // 语言
        format: 'yyyy-mm',       // 日期格式
        weekStart: 1,               // 周一
        todayBtn:  true,            // 是否显示今日按钮
        autoclose: true,            // 选择一个日期后自动关闭时间选择器
        todayHighlight: true,       // 今天高亮显示
        startView: 3,               // 0 hour, 1 day, 2 month, 3 year, 4 10year
        minView: 3,                 // 所能提供的最精准的时间选择视图 0:hour, 1:day, 2:month, 3:year, 4:10 year
        //maxView: 1,               // 所能提供的最高的选额范围视图 4:10year(默认)
        forceParse: true,           // 是否强制解析输入框中的值
        keyboardNavigation: true,   // 是否允许通过方向键改变日期
        pickerPosition: "bottom-right"
    });
}


// 求某一天指定天数前的日期 日期格式为 YYYY-MM-DD
function getDateBeforeSeveralDays(someDate,n) {
    var OneMonth = someDate.substring(5,someDate.lastIndexOf ('-'))
    var OneDay = someDate.substring(someDate.length,someDate.lastIndexOf ('-')+1)
    var OneYear = someDate.substring(0,someDate.indexOf ('-'))
    var mtime = (Date.parse(OneMonth+'/'+OneDay+'/'+OneYear) - n*24*60*60*1000)
    var newTime = new Date(mtime)
    var newYear = newTime.getFullYear()
    var newMonth = (newTime.getMonth() + 1) == 13? 1 : newTime.getMonth() + 1
    var newDay = newTime.getDate()
    return newYear + "-" + setTwoDigit(newMonth) + "-" + setTwoDigit(newDay)
}

// 获取今天的年月日
function getDateToday() {
    var date = new Date()
    var year  = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()
    var yearStr  = year
    var monthStr = setTwoDigit(month)
    var dayStr   = setTwoDigit(day)
    return yearStr + "-" + monthStr + "-" + dayStr
}


// 求某一天指定天数后的日期 日期格式为 YYYY-MM-DD
function getDateAfterSeveralDays(someDate,n) {
    var OneMonth = someDate.substring(5,someDate.lastIndexOf ('-'))
    var OneDay = someDate.substring(someDate.length,someDate.lastIndexOf ('-')+1)
    var OneYear = someDate.substring(0,someDate.indexOf ('-'))
    var mtime = (Date.parse(OneMonth+'/'+OneDay+'/'+OneYear) + n*24*60*60*1000)
    var newTime = new Date(mtime)
    var newYear = newTime.getFullYear()
    var newMonth = (newTime.getMonth() + 1) == 13? 1 : newTime.getMonth() + 1
    var newDay = newTime.getDate()
    return newYear + "-" + setTwoDigit(newMonth) + "-" + setTwoDigit(newDay);
}


// 设置为两位数数字
function setTwoDigit(n) {
    var str = "0"
    if (n < 10) {
        str += n
    } else {
        str = n
    }
    return str
}
